<?php
/**
 * Created by PhpStorm.
 * User: wangjiangtao
 * Date: 2022/2/21
 * Time: 14:58
 */

namespace App\Console\Commands;
use App\Http\Model\Hint;
use App\Http\Model\UserAccountCancel;
use App\Http\Model\UserVerify;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UserCancle extends Command{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'User:cancle';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '用户注销';
    protected $userCancelModel = null;
    protected $userVerifyModel = null;


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->userCancelModel = new UserAccountCancel();
        $this->userVerifyModel = new UserVerify();
    }

    public function handle(){
        //获取已注销但为取消实名用户
        $this->cancleUserVerify();
        //获取注销失败的用户
        $this->cancleUserInfo();
        return '';
    }

    //返回数组
    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }
    //清除 已注销但未取消实名用户
    public function cancleUserVerify(){
        $date = date('Y-m-01 00:00:00' , strtotime(date('Y-m',strtotime('-1 month'))));
        $userData = DB::table('user_account_cancel_log')
            ->leftJoin('user','user_account_cancel_log.user_id','user.user_id')
            ->leftJoin('user_verify','user_account_cancel_log.user_id','user_verify.user_id')
            ->where('user_verify.result_code',1000)
            ->whereRaw('zb_user.user_id = zb_user.mobile_phone')
            ->where('user_account_cancel_log.status',1)
            ->where('user_account_cancel_log.created_time','<=' , $date)
            ->get(['user_account_cancel_log.user_id']);
        $userData = $this->objToArray($userData);
        $userData = array_column($userData,'user_id');
        if (!$userData){
            return '';
        }
        foreach ($userData as $userId){
            try {
                write_log('UserCancle' , '注销身份证', $userId);
                $this->userVerifyModel->setResultEmpty($userId ,0 ,'注销');
                $this->userCancelModel->updateCancelLog($userId);
            }catch (\Exception $e){
                write_log('UserCancle' , '注销身份证Error', $userId);
                write_log('UserCancleError' , '注销身份证Error', $userId);
                continue;
            }
        }

        return '';
    }
    //清除 注销失败的用户
    public function cancleUserInfo(){
        $date = date('Y-m-01 00:00:00' , strtotime(date('Y-m',strtotime('-1 month'))));
        $userData = DB::table('user_account_cancel_log')
            ->leftJoin('user','user_account_cancel_log.user_id','user.user_id')
            ->whereRaw('zb_user.user_id != zb_user.mobile_phone')
            ->where('user_account_cancel_log.status',1)
            ->where('user_account_cancel_log.created_time','<=' , $date)
            ->get(['user_account_cancel_log.user_id']);
        $userData = $this->objToArray($userData);
        $userData = array_column($userData,'user_id');
        if (!$userData){
            return '';
        }
        foreach ($userData as $userId){
            try {
                write_log('UserCancle' , '注销', $userId);
                $this->userCancelModel->cancelUser($userId);
            }catch (\Exception $e){
                write_log('UserCancle' , '注销Error', $userId);
                write_log('UserCancleError' , '注销Error', $userId);
                continue;
            }
        }

        return '';
    }

}
